package com.clz.mycustomview.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

public class FlowLayout extends ViewGroup {
    public FlowLayout(Context context) {
        super(context);
    }

    public FlowLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int measureWidth = MeasureSpec.getSize(widthMeasureSpec);
        int measureHeight = MeasureSpec.getSize(heightMeasureSpec);
        int measureWidthMode = MeasureSpec.getMode(widthMeasureSpec);
        int measureHeightMode = MeasureSpec.getMode(heightMeasureSpec);

        int lineWidth = 0;
        int lineHeight = 0;
        int width = 0;
        int height = 0;
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View childView = getChildAt(i);
            MarginLayoutParams lp = null;
            if (childView.getLayoutParams() instanceof MarginLayoutParams) {
                lp = (MarginLayoutParams) childView.getLayoutParams();
            } else {
                lp = new MarginLayoutParams(0, 0);
            }
            measureChild(childView, widthMeasureSpec, heightMeasureSpec);
            int childWidth = childView.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
            int childHeight = childView.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
            if (childWidth + lineWidth > measureWidth) {
                // 需要换行
                width = Math.max(lineWidth, width);
                height += lineHeight;
                // 由于盛不下当前控件，而将此控件的调到下一行，所以将控件的高度和宽度初始化给lineHeight、lineWidth
                lineWidth = childWidth;
                lineHeight = childHeight;
            } else {
                // 否则累加值lineWidth，lineHeight取最大值
                lineWidth += childWidth;
                lineHeight = Math.max(lineHeight, childHeight);
            }
            // 最后一行是不会超出width范围的，所以需要单独处理
            if (i == count - 1) {
                width = Math.max(lineWidth, width);
                height += lineHeight;
            }
        }
        //当属性是MeasureSpec.EXACTLY时，那么它的高度就是确定的，
        // 只有当是wrap_content时，根据内部控件的大小来确定它的大小时，大小是不确定的，属性是AT_MOST,此时，就需要我们自己计算它的应当的大小，并设置进去
        setMeasuredDimension((measureWidthMode == MeasureSpec.EXACTLY) ? measureWidth
                : width, (measureHeightMode == MeasureSpec.EXACTLY) ? measureHeight
                : height);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int lineWidth = 0;
        int lineHeight = 0;
        int top = 0;
        int left = 0;
        int count = getChildCount();
        Log.i("55565", "count:" + count);
        for (int i = 0; i < count; i++) {
            View childView = getChildAt(i);
            MarginLayoutParams lp = null;
            if (childView.getLayoutParams() instanceof MarginLayoutParams) {
                lp = (MarginLayoutParams) childView.getLayoutParams();
            } else {
                lp = new MarginLayoutParams(0, 0);
            }
            int childWidth = childView.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
            int childHeight = childView.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
            if (childWidth + lineWidth > getMeasuredWidth()) {
                // 如果换行，当前控件将跑到下一行，从最左边开始，所以left就是0，而top则需要加上上一行的行高，才是这个控件的top点
                top += lineHeight;
                left = 0;
                lineWidth = childWidth;
                lineHeight = childHeight;
            } else {
                lineHeight = Math.max(lineHeight, childHeight);
                lineWidth += childWidth;
            }
            // 计算childView的left，top，right，bottom
            int cl = left + lp.leftMargin;
            Log.i("55565", lp.leftMargin + "");
            int ct = top + lp.topMargin;
            int cr = cl + childView.getMeasuredWidth();
            int cb = ct + childView.getMeasuredHeight();
            childView.layout(cl, ct, cr, cb);
            left += childView.getMeasuredWidth()+lp.leftMargin;
        }
    }


    @Override
    protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) {
        return new MarginLayoutParams(lp);
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(), attrs);
    }

    @Override
    protected LayoutParams generateDefaultLayoutParams() {
        return new MarginLayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    }
}
